package com.hljcj.boot.action.sysset;

import com.hljcj.boot.bean.SysPost;
import com.hljcj.boot.service.PostService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/sysset")
public class PostAction {

    @Resource
    private PostService postService;

    @RequestMapping(value = "/initPost")
    public ModelAndView initPost() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sysset/post.html");
        return modelAndView;
    }

    @RequestMapping(value = "/queryPost", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> queryPost(SysPost post, int pageNo, int maxResults) {
        Map<String, Object> map = new HashMap<>();
        try {
            Page<SysPost> page = postService.queryPost(post, pageNo, maxResults);
            map.put("total", page.getTotalElements());
            map.put("rows", page.getContent());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("total", 0);
            map.put("rows", null);
        }
        return map;
    }

    @RequestMapping(value = "/addPost", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, String> addPost(SysPost post) {
        Map<String, String> map = new HashMap<>();
        try {
            String mess = postService.addPost(post);
            map.put("mess", mess);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }


    @RequestMapping(value = "/updPost", method = {RequestMethod.POST, RequestMethod.GET})
    public Map<String, String> updPost(SysPost post) {
        Map<String, String> map = new HashMap<>();
        try {
            String mess = postService.updPost(post);
            map.put("mess", mess);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }

    @RequestMapping(value = "/delPost", method = {RequestMethod.POST})
    public Map<String, String> delPost(Integer postId) {
        Map<String, String> map = new HashMap<>();
        try {
            postService.delPost(postId);
            map.put("mess", "succ");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("mess", e.toString());
        }
        return map;
    }

}